﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ES_Project.Model;
using System.Data;
using System.Data.SqlClient;

using System.Collections;
using System.Web.Security;

using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;

using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace ES_Project.Pages
{
    public partial class WebForm26 : System.Web.UI.Page
    {
        string StrConn = WebConfigurationManager.ConnectionStrings["baseessystem"].ToString();
        MySqlCommand objCmd = new MySqlCommand(); 
        protected void Page_Load(object sender, EventArgs e)
        {
            datadiscount();

        }
        void datadiscount()
        {
            MySqlConnection Conn2 = new MySqlConnection(StrConn);

            string _query = "select * from Discount";
            Conn2.Open();
            MySqlDataAdapter da2 = new MySqlDataAdapter(_query, Conn2);
            DataSet ds2 = new DataSet();
            da2.Fill(ds2);
            da2.Fill(ds2, "Discount");
            GridView2.DataSource = ds2.Tables["Discount"];
            GridView2.DataBind();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            MySqlConnection Conn2 = new MySqlConnection(StrConn);
            string strSQL2;
            strSQL2 = "INSERT INTO Discount (Name_Discount,Num_Discount,Code_school,Name_school,Branch,Address,Iden_number)   VALUES ('" + TextArea1.Value + "' ,'" + Convert.ToInt32(TextBox3.Text) + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextArea2.Value + "','" + TextArea3.Value + "','" + TextBox7.Text + "')";

            MySqlDataAdapter da2 = new MySqlDataAdapter(strSQL2, Conn2);
            Conn2.Open();
            objCmd.Connection = Conn2;
            objCmd.CommandType = CommandType.Text;
            objCmd.CommandText = strSQL2;

            objCmd.ExecuteNonQuery();
            datadiscount();
            clear();
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, "alert('บันทึกข้อมูลเรียบร้อยแล้วค่ะ')", true);
        }

        protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            MySqlConnection Conn = new MySqlConnection(StrConn);
            string strSQL;
            strSQL = "DELETE FROM Discount WHERE id_Discount ='" + GridView2.DataKeys[e.RowIndex].Value + "'";
            Conn.Open();
            objCmd = new MySqlCommand(strSQL, Conn);

            objCmd.ExecuteNonQuery();




            GridView2.EditIndex = -1;

          
            Button1.Enabled = true;
            Button5.Enabled = true;
            datadiscount();
        }

        protected void GridView2_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            Button1.Enabled = false;

            MySqlConnection Conn = new MySqlConnection(StrConn);
            string strSQL;
            strSQL = "SELECT * FROM Discount WHERE id_Discount ='" + GridView2.DataKeys[e.NewSelectedIndex].Value + "'";

            Conn.Open();
            MySqlDataAdapter da4 = new MySqlDataAdapter(strSQL, Conn);
            DataSet ds4 = new DataSet();
            da4.Fill(ds4);

            int _row = ds4.Tables[0].Rows.Count;
            int i = 0;
            if (_row > i)
            {
                TextArea1.Value = (string)ds4.Tables[0].Rows[i]["Name_Discount"].ToString();
                TextBox3.Text = Convert.ToString(ds4.Tables[0].Rows[i]["Num_Discount"]);
                TextBox4.Text = Convert.ToString(ds4.Tables[0].Rows[i]["id_Discount"]);
                TextBox5.Text = (string)ds4.Tables[0].Rows[i]["Code_school"].ToString();
                TextBox6.Text = (string)ds4.Tables[0].Rows[i]["Name_school"].ToString();
                TextArea2.Value = (string)ds4.Tables[0].Rows[i]["Branch"].ToString();
                TextArea3.Value = (string)ds4.Tables[0].Rows[i]["Address"].ToString();
                TextBox7.Text = (string)ds4.Tables[0].Rows[i]["Iden_number"].ToString();
            }
        }

        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // loop all data rows
                foreach (DataControlFieldCell cell in e.Row.Cells)
                {
                    // check all cells in one row
                    foreach (Control control in cell.Controls)
                    {
                        // Must use LinkButton here instead of ImageButton
                        // if you are having Links (not images) as the command button.
                        ImageButton button = control as ImageButton;
                        if (button != null && button.CommandName == "Delete")
                            // Add delete confirmation
                            button.OnClientClick = "if (!confirm('คุณต้องการที่จะลบข้อมูล?')) return;";
                    }
                }
            }
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            string StrConn = WebConfigurationManager.ConnectionStrings["baseessystem"].ToString();
            MySqlConnection objConn = new MySqlConnection();
            MySqlCommand objCmd = new MySqlCommand();

            objConn = new MySqlConnection(StrConn);
            objConn.Open();
            string strSQL;

            strSQL = "UPDATE discount SET " +
       " Name_Discount = '" + TextArea1.Value + "' " +
             " ,Num_Discount = '" + this.TextBox3.Text + "' " +
             " ,Code_school = '" + this.TextBox5.Text + "' " +
              " ,Name_school = '" + this.TextBox6.Text + "' " +
               " ,Branch = '" + TextArea2.Value + "' " +
                " ,Address = '" + TextArea3.Value + "' " +
                " ,Iden_number = '" + TextBox7.Text + "' " +
        " WHERE id_Discount = '" + this.TextBox4.Text + "' ";

            objCmd = new MySqlCommand();
            objCmd.Connection = objConn;
            objCmd.CommandText = strSQL;
            objCmd.CommandType = CommandType.Text;

            objCmd.ExecuteNonQuery();

            datadiscount();
            Button1.Enabled = true;
            Button5.Enabled = false;
            clear();
        }
        void clear()
        {
            TextArea1.Value = "";
            TextBox3.Text = "";
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            TextBox3.Text = "";
            TextBox5.Text = "";
            TextBox6.Text = "";
            TextBox7.Text = "";
            TextArea1.Value = "";
            TextArea2.Value = "";
            TextArea3.Value = "";
        }
    }
}